// pages/case/case.js
const app = getApp()
const api = require('../../utils/api.js');
const config = require('../../config/config.js');
// const util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagePath:"",
    slides:[],
    qa_lst:[],
    case_lst:[],
    cert_lst:[],
    goods_lst:[],
    videos:[],
    swiper_list:[],
    mt_video:[], 
    mt_now:[],
    is_video_open:0,
    bannerCurrent:0,
    
    current_index: 0,//轮播广告下标
    start_x: 0,//初始距离
    target_pixel_value: 0,//尺寸大小
    swiper_style: null,
    interval_time: null,
    autoplay: true,
},
  onSwiperChange(event) {
    const { current, source } = event.detail
    if (source === 'autoplay' || source === 'touch') {
        this.setData({
            current: current
        })
    }

    this.triggerEvent('change', current)
  },
   //首页组装数据
   getData() {
    this.setData({
      loading: true
    })
    api.request('home', 'get').then(res => {
      let data = res.data.data
      this.setData({
        slides:data.slides,
        qa_lst:data.qa_lst,
        case_lst:data.case_lst,
        cert_lst:data.cert_lst,
        goods_lst:data.goods_lst,
        videos:data.videos,
        is_video_open:data.is_video_open,
        imagePath: config.server_img_http
      })
      console.log(data,333333)
    })
  },

     //首页组装数据M
     getMt_nowData() {
      this.setData({
        loading: true
      })
      api.request('mt_now', 'post').then(res => {
        let data = res.data.data
        this.setData({
          mt_now:data.info_lst,
          imagePath: config.server_img_http
        })
      })
    },
    //首页组装数据V
    getMt_videoData() {
      this.setData({
        loading: true
      })
      api.request('mt_video', 'post').then(res => {
        let data = res.data.data
        this.setData({
          mt_video:data.info_lst,
          imagePath: config.server_img_http
        })
      })
    },
  //预览图片
  preview(e) {
    console.log(e)
    let url = e.currentTarget.dataset.url
    wx.previewImage({
      current: '', // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  // 跳转
  toPages(e){
    console.log(e)
    let to = e.currentTarget.dataset.to
    switch(to){
      case 'QA':
        wx.navigateTo({
          url: '/pages/more-warranty/more-warranty',
          fail: function(res) {
            console.log(res)
          }
        });
      break;
      case 'product':
        wx.switchTab({
          url: '/pages/product/product'
        })
      break;
      case 'productInfo':
        wx.navigateTo({
          url: '/pages/product/productInfo/productInfo?id='+e.currentTarget.dataset.id,
          fail: function(res) {
            console.log(res)
          }
        });
      break;
      case 'patent':
      app.globalData.activeId = e.currentTarget.dataset.activeid || null;
      wx.navigateTo({
        url: '/pages/patent/patent',
        fail: function(res) {
          console.log(res)
        }
      });
      break;
      case 'case':
        app.globalData.activeId = e.currentTarget.dataset.activeid || null;
        wx.switchTab({
          url: '/pages/case/case'
        })
      break;
      case 'caseInfo':
        wx.navigateTo({
          url: '/pages/case/case-info/case-info?id='+e.currentTarget.dataset.id,
          fail: function(res) {
            console.log(res)
          }
        });
      break;
      case 'video':
        app.globalData.activeId = e.currentTarget.dataset.activeid || null;
        wx.navigateTo({
          url: '/pages/videoList/videoList',
          fail: function(res) {
            console.log(res)
          }
        });
      break;
    }
  },

  swiper_touch_start(e) {
    const startX = e.changedTouches[0].pageX;
    const res = wx.getSystemInfoSync();
    const screenWidth = res.screenWidth;
    const currentRpxValue = 750;
    const targetRpxValue = 255;
    const targetPixelValue = Math.ceil(screenWidth * targetRpxValue / currentRpxValue) * 2;
    const intervalTime = this.data.interval_time;
    if (intervalTime) {
      clearInterval(intervalTime)
    }
    this.setData({
      start_x: startX,
      target_pixel_value: targetPixelValue,
      autoplay: false,
      interval_time: null
    })
  },
  swiper_touch_move(e) {
    const moveX = e.changedTouches[0].pageX;
    const index = this.data.current_index;
    const dx = this.data.start_x - moveX;
    const targetPixelValue = this.data.target_pixel_value;
    const patio = dx / targetPixelValue;
    const distance = dx < 0 ? Math.floor(patio) : Math.ceil(patio);
    const active_index = distance + index;
    const length = this.data.swiper_list.length - 1;
    //左右限制
    if ((dx < 0 && index === 0) || (dx > 0 && index === length)) return;
    let newActiveIndex = active_index;
    if (active_index <= 0) {
      newActiveIndex = 0;
    }
    if (active_index >= length) {
      newActiveIndex = length;
    }
    //余数比例
    const proportion = Math.abs(patio) % 1;
    if (newActiveIndex === 0 && Math.abs(patio) >= index) return;
    if (newActiveIndex === length && Math.abs(patio) >= 1) return;
    const scale = 0.8 + proportion / 5;
    const scale2 = 1 - proportion / 5;
    const swiper_style = new Array(this.data.swiper_list.length);
    swiper_style.fill("transform:scale(0.8);");
    swiper_style[newActiveIndex] = `transform:scale(${scale >= 0.98 ? 1 : scale});`;
    swiper_style[dx > 0 ? newActiveIndex - 1 : newActiveIndex + 1] = `transform:scale(${scale2 <= 0.82 ? 0.8 : scale2});`;
    this.setData({
      swiper_style
    })
  },
  swiper_touch_end() {
    this.setData({
      start_x: 0,
      target_pixel_value: 0,
      swiper_style: null,
      autoplay: true
    })
  },
  //动画结束回去index0
  animationFinish(e) {
    const index = e.detail.current;
    if (index === this.data.swiper_list.length - 1) {
      let interval = setInterval(() => {
        const index = this.data.current_index;
        if (index === 0) {
          clearInterval(interval);
          interval = null;
          this.setData({
            autoplay: true
          })
          return;
        }
        this.setData({
          current_index: index - 1
        })
      }, 300)
      this.setData({
        autoplay: false,
        interval_time: interval
      })
    }
  },
  change_current(e) {
    const source = e.detail.source;
    if (source === 'touch' || source === 'autoplay') {
      const index = e.detail.current;
      this.setData({
        current_index: index,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData()
    this.getMt_nowData()
    this.getMt_videoData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})